package net.nettytlv;

import net.nettytlv.domain.Packet;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@ChannelHandler.Sharable
public class PacketChannelHandler extends SimpleChannelInboundHandler<Packet> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Packet packet) throws Exception {
        log.debug(packet.toString());

        if (!ctx.channel().hasAttr(Client.KEY)) {
            throw new Exception("Channel not found client.");
        }
        Client client = ctx.channel().attr(Client.KEY).get();

        switch (packet.getType()) {
            case (byte) 2:
                // 心跳响应
                client.resetPingTime();
                break;

            default:
                log.error("Not found type handler. type= {}", packet.getType());
        }
    }
}
